/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.service.impl;

import com.opgea.qms.dao.CompanyDAO;
import com.opgea.qms.dao.DestinationDAO;
import com.opgea.qms.dao.TokenDAO;
import com.opgea.qms.dao.TokenDetailsDAO;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Destination;
import com.opgea.qms.domain.entities.Token;
import com.opgea.qms.domain.entities.TokenDetails;
import com.opgea.qms.domain.qualifiers.PrefixQualifier;
import com.opgea.qms.domain.qualifiers.TokenStatusQualifier;
import com.opgea.qms.listeners.TokenContextLoaderListener;
import com.opgea.qms.service.ApplicationService;
import com.opgea.qms.service.TokenDetailsService;
import com.opgea.qms.web.form.TokenDetailsDTO;
import com.opgea.util.DateUtil;
import java.lang.Integer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class TokenDetailsServiceImpl implements TokenDetailsService{

    @Autowired
    private TokenDetailsDAO tokenDetailsDAO;
    @Autowired
    private TokenDAO tokenDAO;
    @Autowired
    private DestinationDAO destinationDAO;
    @Autowired
    private CompanyDAO companyDAO;
            
    @Autowired
    private QmsSMSImpl smsService;        
    
    @Autowired
    private ApplicationService applicationService;
    
    
    @Override
    public synchronized Integer getNextTokenNumber(Long companyId, String dateString) {
        /*
        CurrentTokenModel currentToken = TokenContextLoaderListener.currentTokenMap.get(companyId);
        Date today = Calendar.getInstance(Locale.US).getTime();
        Date currentTokenDate = currentToken.getToday();
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            today = sdf.parse(sdf.format(today));
            currentTokenDate = sdf.parse(sdf.format(currentTokenDate));
        }catch(ParseException pe){
            System.out.println("Application Service Exception>> getNextTokenNumber"+pe.getLocalizedMessage());
        }
        
        Integer nextTokenNumber = currentToken.getCurrentTokenNumber();
        if(today.compareTo(currentTokenDate) > 0 || nextTokenNumber == null){
            nextTokenNumber = 1;
        }
        else{
            nextTokenNumber = nextTokenNumber+1;
        }
        currentToken.setCurrentTokenNumber(nextTokenNumber);
        currentToken.setToday(today);
        return nextTokenNumber;
         * 
         */
        if(tokenDAO.getMaxTokenNumber(companyId, dateString) == null){
            return 1;
        }else{
            return tokenDAO.getMaxTokenNumber(companyId, dateString)+1;
        }
    }
    
    
    @Override
    public List<TokenDetailsDTO> findAll() {
        List<TokenDetails> tokenList = tokenDetailsDAO.findAll();
        List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
        if(tokenList != null){
            for(TokenDetails tokenDetails: tokenList){
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsList.add(tdm);
            }
        }
        
        return tokenDetailsList;
    }

    @Override
    public List<TokenDetailsDTO> findAllByCompanyId(Long companyId) {
        Company company = companyDAO.find(companyId);
        
        List<Token> tokenList = company.getTokenList();
        
        List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
        if(tokenList != null){
            for(Token token: tokenList){
                //System.out.println("Token: "+token);
                TokenDetails tokenDetails = token.getTokenDetails();
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsList.add(tdm);
            }
        }
        
        return tokenDetailsList;
    }
    
    @Override
    public List<TokenDetailsDTO> findAllByDestinationId(Long companyId, Long destinationId) {
        //System.out.println("Overloaded findAllByDestinationId");
        List<TokenDetails> tokenDetailsList = tokenDetailsDAO.findAllByDestination(companyId, destinationId);
        List<TokenDetailsDTO> tokenDetailsDTOList = new ArrayList<TokenDetailsDTO>();
        
        if(tokenDetailsList != null){
            for(TokenDetails tokenDetails: tokenDetailsList){
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsDTOList.add(tdm);
            }
        }
        
        return tokenDetailsDTOList;
    }

    @Override
    public TokenDetailsDTO create(TokenDetailsDTO tokenDetailsDTO) {
        TokenDetails tokenDetails = new TokenDetails();
        Token token = new Token();

        Company company = companyDAO.find(tokenDetailsDTO.getCompanyId());
        Destination destination = destinationDAO.find(new Long(tokenDetailsDTO.getDestinationId()));
        token.setTokenNo(this.getNextTokenNumber(company.getId(), tokenDetailsDTO.getAppointmentDate()));
        token.setCompany(company);
        token.setTokenDate(DateUtil.getDateFromYYYYMMDD(tokenDetailsDTO.getAppointmentDate(), "-"));
        
        tokenDAO.create(token);
        
        tokenDetails.setFirstName(tokenDetailsDTO.getFirstName());
        tokenDetails.setMiddleInitial(tokenDetailsDTO.getMiddleInitial());
        tokenDetails.setLastName(tokenDetailsDTO.getLastName());
        tokenDetails.setPrefix(PrefixQualifier.Mr.ordinal());
        tokenDetails.setMobileNo(tokenDetailsDTO.getMobileNo());
        tokenDetails.setEmailId(tokenDetailsDTO.getEmailId());
        tokenDetails.setToken(token);
        tokenDetails.setDestination(destination);
        tokenDetails.setTokenStatus(TokenStatusQualifier.NEW);
        tokenDetails.setMetaData(tokenDetailsDTO.getMetaData());
        tokenDetailsDAO.create(tokenDetails);
        applicationService.addTokenStatus(tokenDetails);
        
        System.out.println("Token Context Loader Size..."+TokenContextLoaderListener.currentTokenMap.size());
        
        /*
         * Sending SMS Token Number to Mobile
         *
        try {
            smsService.sendTokenNo(tokenDetails.getMobileNo().toString(), tokenDetails.getToken().getTokenNo().toString());
        } catch (Exception ex) {
            Logger.getLogger(ReceptionController.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
        
        tokenDetailsDTO.setTokenNo(token.getTokenNo().toString());
        return tokenDetailsDTO;
    }

    @Override
    public TokenDetailsDTO update(TokenDetailsDTO tokenDetailsDTO) {
        //System.out.println("TokenDetailsDTO : "+tokenDetailsDTO.getDestinationId());
        TokenDetails tokenDetails = null;
        if(tokenDetailsDTO.getId() != null){
            Destination destination = destinationDAO.find(tokenDetailsDTO.getDestinationId());
            tokenDetails = tokenDetailsDAO.find(tokenDetailsDTO.getId());
            tokenDetails.setFirstName(tokenDetailsDTO.getFirstName());
            tokenDetails.setMiddleInitial(tokenDetailsDTO.getMiddleInitial());
            tokenDetails.setLastName(tokenDetailsDTO.getLastName());
            tokenDetails.setPrefix(PrefixQualifier.Mr.ordinal());
            tokenDetails.setMobileNo(tokenDetailsDTO.getMobileNo());
            tokenDetails.setEmailId(tokenDetailsDTO.getEmailId());
            tokenDetails.setDestination(destination);
            tokenDetails.setTokenStatus(TokenStatusQualifier.values()[tokenDetailsDTO.getTokenStatus()]);
            tokenDetails.setMetaData(tokenDetailsDTO.getMetaData());
            tokenDetailsDAO.update(tokenDetails);
            if(tokenDetailsDTO.getTokenStatus().equals(TokenStatusQualifier.DONE.ordinal()))
            {
                applicationService.removeTokenStatus(tokenDetails);
            }else{
                applicationService.updateTokenStatus(tokenDetails);
            }
        }else{
            this.create(tokenDetailsDTO);
        }
        
        return tokenDetailsDTO;
    }

    @Override
    public TokenDetailsDTO find(Long tokenDetailsId) {
        TokenDetails tokenDetails = tokenDetailsDAO.find(tokenDetailsId);
        TokenDetailsDTO tdm = new TokenDetailsDTO();
        tdm.setTokenNo(tokenDetails.getId().toString());
        tdm.setFirstName(tokenDetails.getFirstName());
        tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
        tdm.setLastName(tokenDetails.getLastName());

        if(tokenDetails.getId() != null){
            tdm.setId(tokenDetails.getId());
        }
        if(tokenDetails.getToken().getTokenNo() != null){
            tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
        }
        if(tokenDetails.getPrefix() != null){
            tdm.setPrefix(tokenDetails.getPrefix().toString());
        }
        if(tokenDetails.getFirstName() != null){
            tdm.setFirstName(tokenDetails.getFirstName());
        }
        if(tokenDetails.getMiddleInitial() != null){
            tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
        }
        if(tokenDetails.getLastName() != null){
            tdm.setLastName(tokenDetails.getLastName());
        }
        if(tokenDetails.getMobileNo() != null){
            tdm.setMobileNo(tokenDetails.getMobileNo());
        }
        if(tokenDetails.getEmailId() != null){
            tdm.setEmailId(tokenDetails.getEmailId());
        }
        if(tokenDetails.getDestination() != null){
            tdm.setDestinationId(tokenDetails.getDestination().getId());
            tdm.setDestinationName(tokenDetails.getDestination().getName());
        }
        if(tokenDetails.getTokenStatus() != null){
            tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
        }
        
        return tdm;
    }

    @Override
    public TokenDetailsDTO findByTokenId(Long tokenId) {
        
        Token token = tokenDAO.find(tokenId);
        TokenDetails tokenDetails = token.getTokenDetails();
        TokenDetailsDTO tdm = new TokenDetailsDTO();
        tdm.setTokenNo(tokenDetails.getId().toString());
        tdm.setFirstName(tokenDetails.getFirstName());
        tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
        tdm.setLastName(tokenDetails.getLastName());

        if(tokenDetails.getId() != null){
            tdm.setId(tokenDetails.getId());
        }
        if(tokenDetails.getToken().getTokenNo() != null){
            tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
        }
        if(tokenDetails.getPrefix() != null){
            tdm.setPrefix(tokenDetails.getPrefix().toString());
        }
        if(tokenDetails.getFirstName() != null){
            tdm.setFirstName(tokenDetails.getFirstName());
        }
        if(tokenDetails.getMiddleInitial() != null){
            tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
        }
        if(tokenDetails.getLastName() != null){
            tdm.setLastName(tokenDetails.getLastName());
        }
        if(tokenDetails.getMobileNo() != null){
            tdm.setMobileNo(tokenDetails.getMobileNo());
        }
        if(tokenDetails.getEmailId() != null){
            tdm.setEmailId(tokenDetails.getEmailId());
        }
        if(tokenDetails.getDestination() != null){
            tdm.setDestinationId(tokenDetails.getDestination().getId());
            tdm.setDestinationName(tokenDetails.getDestination().getName());
        }
        if(tokenDetails.getTokenStatus() != null){
            tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
        }
        
        return tdm;
    }

    @Override
    public List<TokenDetailsDTO> searchTokens(Long companyId, String searchKey, String tokenDate) {
        List<TokenDetails> tokenDetailsList = tokenDetailsDAO.searchTokens(companyId, searchKey, tokenDate);
        List<TokenDetailsDTO> tokenDetailsDTOList = new ArrayList<TokenDetailsDTO>();
        if(tokenDetailsList != null){
            for(TokenDetails tokenDetails: tokenDetailsList){
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                tdm.setAppointmentDate(DateUtil.getYYYYMMDDFromDate(tokenDetails.getToken().getTokenDate(), "-"));
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsDTOList.add(tdm);
            }
        }
        
        return tokenDetailsDTOList;
    }

    
}
